{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3e5249f1-db70-44e9-b41d-3a621135b856",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "\n",
    "df = pd.read_excel('肿瘤数据.xlsx')\n",
    "#划分数据与标签\n",
    "X = df.drop(columns='肿瘤性质') \n",
    "y = df['肿瘤性质']\n",
    "#划分训练集与测试集 8:2划分\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2eb9a9b-dca8-4674-a072-fc301104f5e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9473684210526315"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#朴素贝叶斯模型\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "nb_clf = GaussianNB()\n",
    "nb_clf.fit(X_train,y_train)\n",
    "\n",
    "y_pred = nb_clf.predict(X_test)\n",
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b43712-1688-4caf-8e9b-dc470ef4617a",
   "metadata": {},
   "source": [
    "其他一些算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1a80b6d-708c-40b8-98be-cb4cc4fc7fe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各分类算法准确率：\n",
      "K近邻分类器: 0.9561\n",
      "决策树分类器: 0.9561\n",
      "随机森林分类器: 0.9561\n",
      "支持向量机: 0.9561\n",
      "逻辑回归: 0.9561\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "\n",
    "# 数据标准化\n",
    "scaler = StandardScaler()\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.transform(X_test)\n",
    "\n",
    "\n",
    "models = {\n",
    "    'K近邻分类器': KNeighborsClassifier(n_neighbors=5),\n",
    "    '决策树分类器': DecisionTreeClassifier(random_state=1),\n",
    "    '随机森林分类器': RandomForestClassifier(n_estimators=100, random_state=1),\n",
    "    '支持向量机': SVC(random_state=1),\n",
    "    '逻辑回归': LogisticRegression(random_state=1, max_iter=1000)\n",
    "}\n",
    "\n",
    "print(\"各分类算法准确率：\")\n",
    "for name, model in models.items():\n",
    "    # 对需要标准化的模型使用标准化后的数据\n",
    "    if name in ['K近邻分类器', '支持向量机', '逻辑回归']:\n",
    "        model.fit(X_train_scaled, y_train)\n",
    "        y_pred = model.predict(X_test_scaled)\n",
    "    else:\n",
    "        model.fit(X_train, y_train)\n",
    "        y_pred = model.predict(X_test)\n",
    "    \n",
    "    accuracy = accuracy_score(y_test, y_pred)\n",
    "    print(f\"{name}: {accuracy:.4f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a656a326-0275-4006-a8d4-74fec8b2c656",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
